package com.rockwell.robot2015;

import com.rockwell.robot2015.model.ColorBlobInfo;
import com.rockwell.robot2015.model.OnColorBlobDetectListener;
import com.rockwell.robot2015.views.MazeCameraView;
import com.rockwell.robot2015.views.MazeCameraView.TeachMode;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class ChooseColorActivity extends Activity implements MazeCameraView.OnTeachColorListener {
	public static final String TEACH_MODE = "teachmode";
	
	private MazeCameraView	m_mazeCameraView;

	private OnColorBlobDetectListener m_treasureListener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(boolean present, ColorBlobInfo info, int length,
				int width) {
			//m_treasureDetector.onDetect(present, info, length, width);
		}
	};

	private OnColorBlobDetectListener m_obstacleListener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			//m_obstacleDetector.onDetect(present, info, length, width);
		}
	};

	private OnColorBlobDetectListener m_mazeEntranceGuideListener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			//m_mazeEntranceGuideDetector.onDetect(present, info, length, width);
		}
	};

	private OnColorBlobDetectListener m_dropAreaListener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			//m_dropAreaDetector.onDetect(present, info, length, width);
		}
	};

	private OnColorBlobDetectListener m_outerWallListener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			//m_outerWallDetector.onDetect(present, info, length, width);
		}
	};

	private OnColorBlobDetectListener m_maze1Listener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			//m_maze1Detector.onDetect(present, info, length, width);
		}
	};

	private OnColorBlobDetectListener m_maze2Listener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			//m_maze2Detector.onDetect(present, info, length, width);
		}
	};

	private OnColorBlobDetectListener m_maze3Listener = new OnColorBlobDetectListener() {
		boolean enabled = true;

		@Override
		public boolean isEnabled() {
			return enabled;
		}

		@Override
		public void setEnabled(boolean enabled) {
			this.enabled = enabled;
		}
		
		public void onDetected(final boolean present, final ColorBlobInfo info,
				final int length, final int width) {
			//m_maze3Detector.onDetect(present, info, length, width);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_choose_color);
		
		m_mazeCameraView = (MazeCameraView) this.findViewById(R.id.maze_camera_view);
		m_mazeCameraView.setTeachMode(true);
		m_mazeCameraView.setTeachColorListener(this);
		
		m_mazeCameraView.setTreasureListener(m_treasureListener);
		//m_mazeCameraView.setObstacleListener(m_obstacleListener);
		m_mazeCameraView.setMaze1Listener(m_maze1Listener);
		m_mazeCameraView.setMaze2Listener(m_maze2Listener);
		m_mazeCameraView.setMaze3Listener(m_maze3Listener);
		m_mazeCameraView
				.setMazeEntranceGuideListener(m_mazeEntranceGuideListener);
		m_mazeCameraView.setOuterWallListener(m_outerWallListener);
		m_mazeCameraView.setDropAreaListener(m_dropAreaListener);

		Intent intent = getIntent();
		int mode = intent.getIntExtra(TEACH_MODE, 1);
		m_mazeCameraView.setTeachItem(MazeCameraView.TeachMode.values()[mode]);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.choose_color, menu);
		return true;
	}


	@Override
    public void onPause()
    {
        super.onPause();
        m_mazeCameraView.onDeactivated();
    }

	@Override
	public void onStop() {

		super.onStop();
		m_mazeCameraView.onDeactivated();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		m_mazeCameraView.onActivated();
	}

	@Override
	public void colorSelected(TeachMode mode) {
		ChooseColorActivity.this.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				finish();
			}
			
		});
	}
	
	@Override
	public void finish() {
	  // Prepare data intent 
	  Intent data = new Intent();
	  
	  // Activity finished ok, return the data
	  setResult(RESULT_OK, data);
	  super.finish();
	}
}
